#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
#  Test MaxRunTime
#  Note: this test doesn't really do anything yet. It is used in helping
#    to duplicate bug #1268, but so far not successful ...
#

TestName="maxruntime-test"
JobName=backup

. scripts/functions

copy_test_confs

rm -f ${cwd}/tmp/RUN*log
cp -f ${cwd}/scripts/bacula-dir.conf.maxruntime ${cwd}/bin/bacula-dir.conf
WHEN=`date '+%Y-%m-%d %H:%M:%S'`

echo "${cwd}/build" >${cwd}/tmp/file-list

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
messages
label volume=TestVolume001 pool=PoolA
label volume=TestVolume002 pool=Default
mount
wait
messages
setdebug trace=1 level=60 dir
run job=RUN_MAXRUNTIME pool=PoolA yes
run job=RUN_MAXRUNTIME fileset=FS_TESTJOB pool=PoolA yes
wait
messages
st dir
quit
END_OF_DATA

(mkfifo ${cwd}/tmp/fifo 
 mkfifo ${cwd}/tmp/fifo2
 mkfifo ${cwd}/tmp/fifo3) 2> /dev/null

run_bacula
check_for_zombie_jobs storage=File
stop_bacula

rm -f ${cwd}/tmp/fifo ${cwd}/tmp/fifo2 ${cwd}/tmp/fifo3

dstat=0
bstat=0
rstat=0
touch ${cwd}/tmp/log2.out

grep -e ': Fatal error: Max run time exceeded. Job canceled.' ${cwd}/tmp/log1.out >/dev/null 
if [ $? -eq 0 ]
then
    [ "$debug" = 1 ] && echo MAXRUNTIME ok
else
   echo "MAXRUNTIME in error"
   bstat=1
fi

nb=`grep -e ': Fatal error: Max run time exceeded. Job canceled.' ${cwd}/tmp/log1.out | wc -l`
if [ $nb -eq 1 ]
then
    [ "$debug" = 1 ] && echo MAXRUNTIME ok
else
   echo "MAXRUNTIME in error, should not cancel the second job"
   bstat=1
fi

end_test
